package cn.edu.nju.software.zp13.core2.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectionUtil {
	@SuppressWarnings("unchecked")
	public static <T>T getFieldValue(Field f,Object o){
		T t = null;
		try {
			t=(T) f.get(o);
		} catch (IllegalArgumentException | IllegalAccessException e) {
			e.printStackTrace();
		}
		return t;
	}
	
	
	public static String fieldVallueToString(Field f,Object o){
		try {
			return String.valueOf(f.get(o));
		} catch (IllegalArgumentException | IllegalAccessException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public static <T> T invokeMethod(Method m,Object o,Object... args){
		T t = null;
		try {
			t=(T) m.invoke(o, args);
		} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
			e.printStackTrace();
		}
		return t;
	}
	
	public static Object getValueFromFieldOrMehod(Field f,Method m,Object o){
		if(m!=null){
			return getFieldValue(f, o);
		}else{
			return invokeMethod(m, o, new Object[]{});
		}
	}
	
}
